﻿using System;
using System.Web.Mvc;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Models;
using Kaiser.Models.JsonModels;
using Kaiser.Extensions;
using System.Web.Script.Serialization;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Services;
using Kaiser.Modules.Cubic.ViewModel;
using Kaiser.Modules.Cubic.DomainModel.Advert;

namespace Kaiser.Modules.Cubic.Controllers
{

    public class AdvertManageController : KaiserBaseController
    {
        #region 变量和属性
        private readonly IAdvertSpaceService _advertSpaceService;

        public AdvertManageController(IAdvertSpaceService advertSpaceService, IAdvertService advertService)
        {
            this._advertSpaceService = advertSpaceService;
            _advertService = advertService;
        }

        private readonly IAdvertService _advertService;

       
        #endregion

        #region 广告栏位管理
        public ActionResult ListAdvertSpace()
        {
            return View();
        }

        /// <summary>
        /// 广告栏位列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ListAdvertSpace(AdvertSpaceSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = _advertSpaceService.Search(search, pageinfo);

            return Json(result);
        }

        public ActionResult EditAdvertSpace(string id)
        {
            AdvertSpace model = new AdvertSpace();
            if (!string.IsNullOrEmpty(id))
                model = _advertSpaceService.Get(id);
            return View(model);
        }

        /// <summary>
        /// 编辑广告位
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditAdvertSpace(string id, AdvertSpace model, FormCollection form)
        {
            var result = new JsonReturnMessages();

            try
            {
                if (!string.IsNullOrEmpty(id))
                    _advertSpaceService.Update(model);
                else
                {
                    model.Id = Guid.NewGuid().GenerateGuid().ToString();
                    _advertSpaceService.Insert(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        public ActionResult DelListAdvertSpace(string ids) 
        {
            var result = new JsonReturnMessages();
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {

                    _advertSpaceService.Delete(new string[]{ids});
                    result.success = true;
                    result.msg = "删除成功！";

                }
                catch (Exception ex)
                {
                    result.success = false;
                    result.msg = ex.Message;

                }
            }
            else
            {
                result.success = false;
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }
        #endregion

        #region 广告管理
        public ActionResult ListAdvert(string id)
        {
            ViewBag.SpaceId = id;
            return View();
        }

        [HttpPost]
        public ActionResult ListAdvert(AdvertSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = _advertService.Search(search, pageinfo);
            return Json(result);
        }


        public ActionResult EditAdvert()
        {
            string id = Request.QueryString["id"];
            string SpaceId = Request.QueryString["SpaceId"];
            Advert model = new Advert();
            model.SpaceId = SpaceId;
            if (!string.IsNullOrEmpty(id))
                model = _advertService.Get(id);
            return View(model);
        }

        public ActionResult DelAdvert(string ids) 
        {
            var result = new JsonReturnMessages();
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {

                    _advertService.Delete(ids);
                    result.success = true;
                    result.msg = "删除成功！";

                }
                catch (Exception ex)
                {
                    result.success = false;
                    result.msg = ex.Message;

                }
            }
            else
            {
                result.success = false;
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        /// <summary>
        /// 编辑广告
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditAdvert(string id, Advert model, FormCollection form) 
        {
            var result = new JsonReturnMessages();

            try
            {
                if (!string.IsNullOrEmpty(id)){
                    Advert oldSort = _advertService.Get(id);
                    model.CreateDateTime = oldSort.CreateDateTime;
                    model.CreateUserFullName = oldSort.CreateUserFullName;
                    model.CreateUserName = oldSort.CreateUserName;
                    model.LastModifyDateTime = DateTime.Now;
                    model.LastModifyUserFullName = User.Identity.Name;
                    model.LastModifyUserName = CurrentUser.Name;
                    _advertService.Update(model);
                }
                else
                {
                    model.Id = Guid.NewGuid().GenerateGuid().ToString();
                    model.CreateDateTime = DateTime.Now;
                    model.CreateUserFullName = User.Identity.Name;
                    model.CreateUserName = CurrentUser.Name;
                    _advertService.Insert(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }
        #endregion
    }
}
